﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using Module.DataAccess;
using Module.EntitiesInfo;


namespace Module.BusinessLogic
{
    public class CMSContentsBL
    {     
        public static int Insert(CMSContentsInfo cMSContentsInfo)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
                return new CMSContentsDB().Insert(cMSContentsInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
     
        public static void Update(CMSContentsInfo cMSContentsInfo)
        {
            try
            {
                new CMSContentsDB().Update(cMSContentsInfo);
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
            }
            catch (Exception ex)
            {
                throw;
            }

        }
      
        public static void Delete(int contentID)
        {
            try
            {
                new CMSContentsDB().Delete(contentID);
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        public static void UpdateStatus(string contentID, int check)
        {
            try
            {
                new CMSContentsDB().UpdateStatus(contentID, check);
            }
            catch (Exception ex)
            {
                throw;
            }

        }            
   
        public static DataTable SelectSearchAll(int status, int lang, int zoneId, string keyword, int createBy, System.DateTime startDate, System.DateTime endDate, int from, int to)
        {
            try
            {
                return new CMSContentsDB().SelectSearchAll(status, lang, zoneId, keyword, createBy, startDate, endDate, from, to);
            }
            catch (Exception ex)
            {
                throw;
            }

        }       

        /// <summary>
        /// Hiển thị bài viết theo chuyên mục, bài viết nổi bật, hot,....
        /// </summary>
        /// <param name="lang">Ngôn ngữ:tiếng việt=1,tiếng anh =2</param>
        /// <param name="topN">Số bản ghi cần lấy</param>
        /// <param name="zoneID">Lấy theo chuyên mục</param>
        /// <param name="status">=5 hiển thị</param>
        /// <param name="showType">lấy theo bài viết nổi bật, hot:showType= 2,showType= 4,showType= 8,showType= 18</param>
        /// <param name="type">type =1 hoặc type =2</param>
        /// <returns></returns>
        public static DataTable GetContentTypeLates(int lang, int topN, int zoneID, int status, string showType, int type)
        {
            try
            {
                return new CMSContentsDB().GetContentTypeLates(lang, topN, zoneID, status, showType, type);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static DataTable GetAll(int topN, int zoneID, int status, int param)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                string strCache = topN + "_" + zoneID + "_" + status + "_" + param + "{7F04D705-3125-4A9C-983A-123170FABC07}";
                DataTable dt = new DataTable();
                if (cache.GetCache(strCache) != null)
                {
                    dt = (DataTable)(cache.GetCache(strCache));
                    return dt;
                }
                dt = new CMSContentsDB().GetAll(topN,zoneID,status,param);
                if (dt != null && dt.Rows.Count > 0)
                {
                    cache.InsertCache(strCache, dt, Convert.ToDouble(ConfigurationSettings.AppSettings["timeCache"].ToString()));
                }
                return dt;               
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static CMSContentsInfo SelectInfo(int type, string param_1, string param_2)
        {
            try
            {
                return ObjectHelper.FillObject<CMSContentsInfo>(new CMSContentsDB().Select(type, param_1, param_2));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static CMSContentsInfo SelectInfoOutsite(int type, string param_1, string param_2)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                string strCache = type + "_" + param_1 + "_" + param_2 + "_" + "{8A5C5D8E-533B-467B-9698-66E5A4131BD0}";
                CMSContentsInfo info=new CMSContentsInfo();
                if (cache.GetCache(strCache) != null)
                {
                    info = (CMSContentsInfo)(cache.GetCache(strCache));
                    return info;
                }
                info = ObjectHelper.FillObject<CMSContentsInfo>(new CMSContentsDB().Select(type, param_1, param_2));
                if (info != null)
                {
                    cache.InsertCache(strCache, info, Convert.ToDouble(ConfigurationSettings.AppSettings["timeCache"].ToString()));
                }
                return info;  
                
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static List<CMSContentsInfo> Select(int type, string param_1, string param_2)
        {
            try
            {
                return ObjectHelper.FillCollection<CMSContentsInfo>(new CMSContentsDB().Select(type, param_1, param_2));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public static List<CMSContentsInfo> Select(int type, string param_1)
        {
            try
            {
                return ObjectHelper.FillCollection<CMSContentsInfo>(new CMSContentsDB().Select(type, param_1));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public static List<CMSContentsInfo> Select(int type)
        {
            try
            {
                return ObjectHelper.FillCollection<CMSContentsInfo>(new CMSContentsDB().Select(type));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static DataTable SelectTbl(int? type, string para_1, string para_2)
        {
            try
            {
                return new CMSContentsDB().SelectTbl(type, para_1, para_2);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static DataTable SelectTbl(int? type, string para_1)
        {
            try
            {
                return new CMSContentsDB().SelectTbl(type, para_1);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public static DataTable SelectTbl(int? type)
        {
            try
            {
                return new CMSContentsDB().SelectTbl(type);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static int GetCountContent(int status, int lang, int zoneId, string keyword, int createBy, DateTime startDate,
                         DateTime endDate)
        {
            try
            {
                return new CMSContentsDB().GetCountContent(status, lang, zoneId, keyword, createBy, startDate, endDate);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

    }
}

